/*
 *  Copyright 2009 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.fs.mybatis.core.generator.mybatis3.javamapper;

import static com.fs.mybatis.core.generator.internal.util.StringUtility.stringHasValue;

import java.util.ArrayList;
import java.util.List;

import com.fs.mybatis.core.api.CommentGenerator;
import com.fs.mybatis.core.api.dom.java.CompilationUnit;
import com.fs.mybatis.core.api.dom.java.FullyQualifiedJavaType;
import com.fs.mybatis.core.api.dom.java.Interface;
import com.fs.mybatis.core.api.dom.java.JavaVisibility;
import com.fs.mybatis.core.generator.AbstractJavaClientGenerator;
import com.fs.mybatis.core.generator.AbstractXmlGenerator;
import com.fs.mybatis.core.generator.config.PropertyRegistry;
import com.fs.mybatis.core.generator.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
import com.fs.mybatis.core.generator.mybatis3.xmlmapper.XMLMapperGenerator;

/**
 * @author Jeff Butler
 * 
 */
public class JavaMapperGenerator extends AbstractJavaClientGenerator {

    /**
     * 
     */
    public JavaMapperGenerator() {
        super(true);
    }

    public JavaMapperGenerator(boolean requiresMatchedXMLGenerator) {
        super(requiresMatchedXMLGenerator);
    }
    
    @Override
    public List<CompilationUnit> getCompilationUnits() {

        CommentGenerator commentGenerator = context.getCommentGenerator();

        FullyQualifiedJavaType type = new FullyQualifiedJavaType(
                introspectedTable.getMyBatis3JavaMapperType());
        Interface interfaze = new Interface(type);
        interfaze.setVisibility(JavaVisibility.PUBLIC);
        interfaze.addAnnotation("@ReadWriteRepository");
        commentGenerator.addJavaFileComment(interfaze);
        
        String allname = introspectedTable.getBaseRecordType();
        String modelname = allname.substring(allname.lastIndexOf(".")+1);
        
        
        String rootInterface = "com.un.general.dao.UNDao<"+ modelname+", Long>";
        if (!stringHasValue(rootInterface)) {
            rootInterface = context.getJavaClientGeneratorConfiguration()
                .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
        }

        if (stringHasValue(rootInterface)) {
            FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(
                    rootInterface);
            interfaze.addSuperInterface(fqjt);
            interfaze.addImportedType(fqjt);
        }
        
       
        FullyQualifiedJavaType modeltype = new FullyQualifiedJavaType(allname);
        // 引入model
        interfaze.addImportedType(modeltype);
        // 引入 com.un.general.common.utils.stereotype.WriterRepository
        interfaze.addImportedType(new FullyQualifiedJavaType("com.un.general.common.stereotype.ReadWriteRepository"));
        
//        addDeleteByPrimaryKeyMethod(interfaze);
//        addInsertMethod(interfaze);
//        addSelectByPrimaryKeyMethod(interfaze);
//        addUpdateByPrimaryKeySelectiveMethod(interfaze);
//        addUpdateByPrimaryKeyWithBLOBsMethod(interfaze);
//        addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze);

        List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
       

        answer.add(interfaze);
        return answer;
    }

    

    protected void initializeAndExecuteGenerator(
            AbstractJavaMapperMethodGenerator methodGenerator,
            Interface interfaze) {
        methodGenerator.setContext(context);
        methodGenerator.setIntrospectedTable(introspectedTable);
        methodGenerator.setProgressCallback(progressCallback);
        methodGenerator.setWarnings(warnings);
        methodGenerator.addInterfaceElements(interfaze);
    }

    public List<CompilationUnit> getExtraCompilationUnits() {
        return null;
    }

    @Override
    public AbstractXmlGenerator getMatchedXMLGenerator() {
        return new XMLMapperGenerator();
    }
}
